package org.jingle.sketch.component {
	import flash.display.Loader;
	
	import org.jingle.sketch.GraphicsUtils;
	
	public class Background extends Composite {
		private var _width:int;
		private var _height:int;
		private var _spacing:int = 20;
		private var _lineColor:uint;
		
		public function Background(parent:Composite, origin:Point,width:int, height:int, backgroundColor:uint = 0xFFFFFF, lineColor:uint = 0x000000) {
			super(parent);
			this.origin = origin;
			this._width = width;
			this._height = height;
			this.backgroundColor = backgroundColor;
			this._lineColor = lineColor;
			//dragEnable = true;
		}
		
		public override function init():void {
			removeAllChildren();
			
			if(backgroundEnable){
				if(backgroundImage != null){
					GraphicsUtils.loadImage(backgroundImage, function(image:Loader):void{
						image.x = origin.x;
						image.y = origin.y;
						sprite.addChild(image);
					}, this);
				}else{
					this.sprite.graphics.beginFill(backgroundColor, alpha);
					this.sprite.graphics.drawRect(origin.x, origin.y, width, height);
					this.sprite.graphics.endFill();
					if(dragEnable){
						this.sprite.buttonMode = true;
					}
					var horizonCount:int = width / _spacing;
			
					for(var i:int=0; i<=horizonCount; i++){
						var x:int = origin.x + i * spacing;
						var line:Line = new Line(this, x, origin.y, x, origin.y + height);
						if(i % 5 == 0){
							line.thickness = 2;
						}else{
							line.thickness = 1;
							line.dashed = true;
						}
						line.lineColor = _lineColor;
						line.alpha = this.alpha;
						if(dragEnable){
							line.sprite.buttonMode = true;
						}
						addChild(line);
					}
					
					var verticalCount:int = height / _spacing;
					for(var j:int=0; j<=verticalCount; j++){
						var y:int = origin.y + j * spacing;
						line = new Line(this, origin.x, y, origin.x + width, y);
						if(j % 5 == 0){
							line.thickness = 2;
						}else{
							line.thickness = 1;
							line.dashed = true;
						}
						line.lineColor = _lineColor;
						line.alpha = this.alpha;
						if(dragEnable){
							line.sprite.buttonMode = true;
						}
						addChild(line);
					}
				}
			}
			initialized = true;
		}
		
		public function set width(width:int):void {
			this._width = width;
		}
		
		public function get width():int
		{
			return this._width;
		}
		
		public function set height(height:int):void {
			this._height = height;
		}
		
		public function get height():int {
			return this._height;
		}
		
		public function set spacing(s:int): void {
			this._spacing = s;
		}
		
		public function get spacing(): int {
			return this._spacing;
		}
	}
}